// Optimizations.

#ifndef LIGHTY_OPTI_H
#define LIGHTY_OPTI_H

#include <GL/glew.h>
#include <SDL_image.h>
#include <string>
#include "Lighty_Debug.h"
#include "Lighty_Sprites_Loader.h"
#include "Lighty_Window_Handler.h"

#define ALLIMG 1
#define NOCHECKNAME 0

int Lighty_Offset(){

  return 0;

}

int Lighty_Preload_Img(std::string name, std::string path, int dupliname=2){
	
  bool found=false;
	
  if(dupliname==2){
	  
    for(int i=0;i<MaxPreloadSprite;i++){
		
	  if(PreloadContainer[i].spritename==name){
		
		Lighty_PrintDebug("Optimization error : Name detected twice, cant assign image !");
	    return 1;
		  
	  }
	  
    }
	  
  }

  for(int i=0;i<MaxPreloadSprite;i++){
	
    if(!PreloadContainer[i].istaken){
		
	  Preloaded_Cache_Sprite[i]=IMG_Load(path.c_str()); // Les gros fichiers peuvent mener à une insuffisance de mémoire.
	  
	  if(Preloaded_Cache_Sprite[i]==NULL){
		
	    Lighty_PrintDebug("Optimization error : An image could not be loaded !");
	    return 1;
		  
	  }
	  	
	  PreloadContainer[i].istaken=true;
	  PreloadContainer[i].spritename=name;
	  std::cout<<i<<std::endl; // À enlever.
	  found=true;
	  i=MaxPreloadSprite;
		
	}
	  
  }
  
  if(!found){
	  
    Lighty_PrintDebug("Optimization error : Too much images have been loaded !");  
    return 1;
	  
  }
  
  return 0;  

}

int Lighty_PreloadFree_Img(std::string name="", int all=0){

  bool found=false;
  
  if(all==1){
	  
    for(int i=0;i<MaxPreloadSprite;i++){
		
      PreloadContainer[i].istaken=false;
	  PreloadContainer[i].spritename.clear();
	  SDL_FreeSurface(Preloaded_Cache_Sprite[i]);
	  SDL_DestroyTexture(Preloaded_Sprite[i]);
	  Preloaded_Cache_Sprite[i]=NULL;
      Preloaded_Sprite[i]=NULL;
		
	}
	  
  }
  
  else{

    for(int i=0;i<MaxPreloadSprite;i++){
	
      if(PreloadContainer[i].spritename==name){
	
	    PreloadContainer[i].istaken=false;
	    PreloadContainer[i].spritename.clear();
	    SDL_FreeSurface(Preloaded_Cache_Sprite[i]);
	    SDL_DestroyTexture(Preloaded_Sprite[i]);
	    Preloaded_Cache_Sprite[i]=NULL;
	    Preloaded_Sprite[i]=NULL;
	    found=true;
        i=MaxPreloadSprite;
		
	  }
	  
    }
    
    if(!found){
	  
	  Lighty_PrintDebug("Optimization error : Name not found, cant free image !");  
      return 1;
	  
    }
    
  }  
  
  return 0;
	
}

#endif 
